#pragma once
//#include <fmod.h>
#include <Windows.h>

#include <fmod.hpp>

enum SoundKey{//Just good for reference
	sClick
};

class SoundEngine
{
private:
	SoundEngine():SoundStreaming(false){};
	FMOD_CAPS			caps;
	FMOD_RESULT			result;
	FMOD::Sound*		snd_Click;


	FMOD::System*		system;
	FMOD_SPEAKERMODE	speakermode;
	FMOD::Sound*		CurrentSong;
	FMOD::Channel*		myChannel;

	bool SoundStreaming;

	int					numdrivers;
	unsigned int		version;
	char				name[256];

public:
	inline static SoundEngine* Into()      
	{
		static SoundEngine engine;
		return &engine;
	}
	void SoundInit()
	{
		//This brick of code skims through the system getting info on what
		//the computer is running under the hood in terms of a sound system
		//It isn't very good however, because it has no asserts, I removed them 
		//Because their example was merely psuedocode and I didn't feel like doing
		//What I considered unnecessary code for the situation
		FMOD::System_Create(&system);
		system->getVersion(&version);
		system->getNumDrivers(&numdrivers);
		if(numdrivers == 0)	{system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);}
		else{
			system->getDriverCaps(0, &caps, 0, 0, &speakermode);
			system->setSpeakerMode(speakermode);
			system->getDriverInfo(0, name, 256, 0);
			result = system->init(100, FMOD_INIT_NORMAL, 0);
			if(caps & FMOD_CAPS_HARDWARE_EMULATED)	
			{system->setDSPBufferSize(1024, 10);}
			if(strstr(name, "SigmaTel"))
			{system->setSoftwareFormat(48000, 
			FMOD_SOUND_FORMAT_PCMFLOAT, 0, 0, FMOD_DSP_RESAMPLER_LINEAR);}
			if(result = FMOD_ERR_OUTPUT_CREATEBUFFER)
			{system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
			system->init(100, FMOD_INIT_NORMAL, 0);}
		}
	}
	void SoundCreate()
	{
		system->createSound("SoundFx/Click.wav",	FMOD_DEFAULT, 0, &snd_Click);
		system->createStream("Music/The Warning.mp3",
			FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, & CurrentSong);
	}
	void SoundPlay(int WhichSound)
	{
		switch(WhichSound)
		{
		case sClick:
			system->playSound(FMOD_CHANNEL_FREE,snd_Click, false, 0);	break;
		}
	}
	void SoundStream()
	{
		if(!SoundStreaming){
			system->playSound				//Pauses the sound because true 
				(FMOD_CHANNEL_FREE, CurrentSong, true, & myChannel);
			myChannel->setVolume(2.0f);		// Set the volume while it is paused.
			myChannel->setPaused(false);	// This is where the sound really starts.
			SoundStreaming = true;
		}
	}
	void SoundUpdate()
	{

		system->update();
		
	}

	~SoundEngine()
	{
		system->release();
	}
};

